<template>
  <main>
    <div class="shop">
      <div class="left">
        <div class="shop-left">
          <li
            v-for="(item,index) in dataList"
            :key="index"
            :class="{on:index==selectIndex}"
            @click="changeIndex(index)"
          >
            {{item.title}}
            <b style="color:red" v-if="item.numm!=0">{{item.numm}}</b>
          </li>
        </div>
      </div>
      <div class="shopRight">
        <div class="right">
          <div class="shop-right">
            <li v-for="(item,index) in dataList" :key="index" ref="li">
              <div class="top">{{item.title}}</div>
              <div class="con">
                <div class="con-item" v-for="(item,ind) in item.items" :key="ind">
                  <div class="img">
                    <img :src="item.imgUrl" alt>
                  </div>
                  <div class="center">
                    <div>{{item.title}}</div>
                    <p>{{item.con}}</p>
                    <div class="bottom">
                      <b style="color:red">￥:{{item.pic}}</b>
                      <div class="div">
                        <span @click="add('-',item.num,index,ind)">-</span>
                        <span>{{item.num}}</span>
                        <span @click="add('+',item.num,index,ind)">+</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </li>
          </div>
        </div>
      </div>
    </div>
  </main>
</template>
<script>
import BScroll from "better-scroll";

import { mapActions, mapState,mapMutations } from "vuex";
export default {
  props: {},
  components: {},
  data() {
    return {
      selectIndex: 0
    };
  },
  computed: {
    ...mapState(["dataList"])
  },
  methods: {
    ...mapActions(["getDataFn", "addFn"]),
    //左边滚动
    scrollLeft() {
      this.leftScroll = new BScroll(".left", { click: true });
    },
    //右边滚动
    scrollRight() {
      this.rightSCroll = new BScroll(".shopRight", { click: true });
    },
    //左边改变颜色
    changeIndex(index) {
      this.selectIndex = index;
      let el = this.$refs.li[index];
      this.rightSCroll.scrollToElement(el, 2000);
    },
    //加加减减
    add(str, num, index, ind) {
      if (str == "-") {
        num--;
        if (num < 0) return;
      } else {
        num++;
      }
      let obj = {
        num,
        index,
        ind
      };
      this.addFn(obj);
    },
      ...mapMutations(["changeIsShow"])
  },
  mounted() {
    //左边滚动
    this.scrollLeft();
    //右边滚动
    this.scrollRight();
    //调用初始的数据
    this.getDataFn();
    this.changeIsShow(true);
  }
};
</script>
<style scoped lang="">
main {
  flex: 1;
  overflow: hidden;
}
.shop {
  width: 100%;
  height: 100%;
  display: flex;
  list-style: none;
}
.shop-left {
  width: 100px;
  background: #eeee;
}

.shop-left li {
  text-align: center;
  line-height: 40px;
  width: 100%;
  height: 40px;
  margin-top: 10px;
  position: relative;
}
.shop-left li.on {
  color: red;
  background: #fff;
}
.shop-left li b {
  width: 10px;
  height: 10px;
  display: block;
  position: absolute;
  top: 0;
  right: 0;
  font-size: 14px;
}

.shop-right {
  width: 260px;
  height: 100%;
  margin-left: 15px;
}
.shop-right li {
  width: 100%;
  margin-top: 10px;
}
.shop-right li .top {
  width: 100%;
  height: 20px;
}
.con {
  width: 100%;
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  margin-top: 10px;
  border-bottom: 1px solid #ccc;
}
.con-item {
  display: flex;
  margin-top: 10px;
  border-bottom: 1px solid #ccc;
}
.con-item .img {
  width: 100px;
  height: 120px;
}
.img img {
  width: 100%;
  height: 100%;
}
.con .center {
  display: flex;
  flex-direction: column;
}
.bottom {
  width: 100%;
  display: flex;
}
.bottom .div {
  width: 120px;
  height: 100%;
  margin-left: 5px;
}
.bottom .div span {
  width: 30px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  border: 1px solid #ccc;
  display: inline-block;
  border-radius: 50%;
}
</style>
